package com.braveman.im.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.bean.CardBean;
import com.braveman.im.bean.SearchBean;
import com.braveman.im.ui.adapter.SearchAdapter;
import com.braveman.im.ui.dialog.TipsDialog;
import com.braveman.im.ui.weiget.DiaogPopup;
import com.braveman.im.utils.UtilsStyle;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/17.
 */

/**
 * 名片搜索
 */
public class SearchActivity extends BaseActivity {
    private ListView lv;
    private SearchBean searchBean;
    private List<SearchBean> list = new ArrayList<SearchBean>();
    private SearchAdapter searchAdapter;
    private String[] industys = {"拨打电话", "修改信息", "分享名片"};
    private String[] phones = {"120345", "567989", "567889"};
    private DiaogPopup diaogPopup;
    private TipsDialog mTipsDialog;
    public static final int REQUEST_PERMISSION_CALL = 0;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("名片搜索").setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(SearchActivity.this);
            }
        });
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_search;
    }

    @Override
    protected void initChildView() {
//        lv = (ListView) findViewById(R.id.lv_activity_search);
    }

    @Override
    protected void initChildData() {
//        searchBean = new SearchBean();
//        searchBean.name = "321312";
//        searchBean.duty = "*******";
//        searchBean.company_name = "********";
//        list.add(searchBean);
//        list.add(searchBean);
//        list.add(searchBean);
//        list.add(searchBean);
//        list.add(searchBean);
//        searchAdapter = new SearchAdapter(this, list);
//        lv.setAdapter(searchAdapter);
//        searchAdapter.notifyDataSetChanged();
    }

    @Override
    protected void setChildViewListener() {

    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        super.onEvent(eventCode, data);
        switch (eventCode) {
            case EventCode.EVENT_REFRESH_LIST_SEARCH:
                SearchBean sb = (SearchBean) data;
                if (list.get(sb.position).equals(searchBean)) {
                    Log.i("123", "名片搜索");
                    Log.i("postiton", list.get(sb.position).toString());
                    DiaogPopup.Builder builder = new DiaogPopup.Builder(SearchActivity.this);
                    int count = 0;
                    for (String industy : industys) {
                        builder.addItem(count++, industy);
                    }
                    diaogPopup = builder.build();
                    diaogPopup.setOnListPopupItemClickListener(new DiaogPopup.OnListPopupItemClickListener() {
                        @Override
                        public void onItemClick(int what) {
                            if (what == 0) {
                                if (diaogPopup != null) {
                                    diaogPopup.dismiss();
                                    call(phones[what], what);//拨打电话
                                }
                            } else if (what == 1) {//修改信息
                                ActivityUtils.startActivity(SearchActivity.this, CompileActivity.class);
                            } else if (what == 2) {//分享名片

                            }
                        }
                    });
                    lv.getSelectedItem();
                    diaogPopup.showPopupWindow(sb.position);//popuwindow特效
                }
                break;
        }

    }

    private void call(final String phone, int type) {
        mTipsDialog = new TipsDialog(this);
        if (type == 0) {
            mTipsDialog.setTitileHint("是否拨打专属销售电话\n");
        } else {
            mTipsDialog.setTitileHint("是否拨打客服电话\n");
        }
        mTipsDialog.show();
        mTipsDialog.setOnDialogTipsClickListener(new TipsDialog.OnDialogTipsClickListener() {

            @Override
            public void doEnter() {
                mTipsDialog.dismiss();
            }

            @Override
            public void doCancel() {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + phone);
                intent.setData(data);
                if (ActivityCompat.checkSelfPermission(SearchActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(SearchActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PERMISSION_CALL
                    );
                    return;
                }
                startActivity(intent);
                mTipsDialog.dismiss();
            }
        });
    }

}
